package com.cloudcentral.controller;

import com.cloudcentral.util.Result;
import com.cloudcentral.service.DetectStrategyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * <p>
 * 探测任务24小时策略表 前端控制器
 * </p>
 *
 * @author Administrator
 * @since 2023-11-23
 */
@Slf4j
@RestController
@RequestMapping("/detect-strategy")
@Tag(name = "探测任务策略")
public class DetectStrategyController extends BaseController {
  private final DetectStrategyService detectStrategyService;

  @Autowired
  public DetectStrategyController(DetectStrategyService detectStrategyService) {
    this.detectStrategyService = detectStrategyService;
  }

  @PostMapping("/modify")
  @Operation(summary = "修改", parameters = {
      @Parameter(name = "id", description = "ID", required = true),
      @Parameter(name = "detectId", description = "任务ID", required = true),
      @Parameter(name = "hour", description = "时段", required = true),
      @Parameter(name = "power", description = "功率", required = true),
      @Parameter(name = "...", description = "更多任意参数"),
  })
  public Result<Boolean> modify(HttpServletRequest request) throws Exception {
    Map<String, Object> params = new HashMap<>();
    Iterator<String> iterator = request.getParameterNames().asIterator();
    while (iterator.hasNext()) {
      String key = iterator.next();
      params.put(key, request.getParameter(key));
    }
    return Result.ok(detectStrategyService.modify(params));
  }
}
